home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap12 / RegionDemo / RegionDemo.cpp next >
C/C++ Source or Header  |  1996-04-05  |  2KB  |  84 lines

  1. //***********************************************************************
  2. //
  3. //  RegionDemo.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include <math.h>
  9. #include "RegionDemo.h"
  10.  
  11. #define FONTHEIGHT 72
  12.  
  13. CMyApp myApp;
  14.  
  15. /////////////////////////////////////////////////////////////////////////
  16. // CMyApp member functions
  17.  
  18. BOOL CMyApp::InitInstance ()
  19. {
  20.     m_pMainWnd = new CMainWindow;
  21.     m_pMainWnd->ShowWindow (m_nCmdShow);
  22.     m_pMainWnd->UpdateWindow ();
  23.     return TRUE;
  24. }
  25.  
  26. /////////////////////////////////////////////////////////////////////////
  27. // CMainWindow message map and member functions
  28.  
  29. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  30.     ON_WM_PAINT ()
  31. END_MESSAGE_MAP ()
  32.  
  33. CMainWindow::CMainWindow ()
  34. {
  35.     Create (NULL, "Region Demo");
  36. }
  37.  
  38. void CMainWindow::OnPaint ()
  39. {
  40.     CPaintDC dc (this);
  41.  
  42.     // Create a 72-point Times New Roman font
  43.     CFont font;
  44.     int nHeight = -((dc.GetDeviceCaps (LOGPIXELSY) * FONTHEIGHT) / 72);
  45.  
  46.     font.CreateFont (nHeight, 0, 0, 0, FW_BOLD, TRUE, 0, 0,
  47.         DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  48.         DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Times New Roman");
  49.  
  50.     // Create a clipping region from the text string "Hello, MFC"
  51.     CRect rect;
  52.     GetClientRect (&rect);
  53.     CString string ("Hello, MFC");
  54.  
  55.     CFont* pOldFont = dc.SelectObject (&font);
  56.     CSize size = dc.GetTextExtent (string);
  57.     int x = (rect.Width () - size.cx) / 2;
  58.     int y = (rect.Height () + nHeight) / 2;
  59.  
  60.     dc.BeginPath ();
  61.     dc.TextOut (x, y, string);
  62.     dc.EndPath ();
  63.     dc.SelectObject (pOldFont);
  64.  
  65.     CRect rcText;
  66.     CRgn rgn1, rgn2;
  67.     rgn1.CreateFromPath (&dc);
  68.     rgn1.GetRgnBox (&rcText);
  69.     rgn2.CreateRectRgnIndirect (&rcText);
  70.     rgn1.CombineRgn (&rgn2, &rgn1, RGN_DIFF);
  71.  
  72.     dc.SelectClipRgn (&rgn1);
  73.  
  74.     // Draw a radial array of lines
  75.     dc.SetViewportOrg (rect.Width () / 2, rect.Height () / 2);
  76.     double fRadius = hypot (rect.Width () / 2, rect.Height () / 2);
  77.  
  78.     for (double fAngle = 0.0; fAngle < 6.283; fAngle += 0.01745) {
  79.         dc.MoveTo (0, 0);
  80.         dc.LineTo ((int) ((fRadius * cos (fAngle)) + 0.5),
  81.             (int) ((fRadius * sin (fAngle)) + 0.5));
  82.     }
  83. }
  84.